package math

import (
	"lesson01/instructions/base"
	"lesson01/rtda"
)

type DDiv struct {
	base.NoOperandsInstruction
}

func (e *DDiv) Execute(frame *rtda.Frame) {
	stack := frame.OperandStack()
	v2 := stack.PopDouble()
	v1 := stack.PopDouble()

	if v2 == 0 {
		panic("java.lang.ArithmeticException: / by zero")
	}

	stack.PushDouble(v1 / v2)
}

type FDiv struct {
	base.NoOperandsInstruction
}

func (e *FDiv) Execute(frame *rtda.Frame) {
	stack := frame.OperandStack()
	v2 := stack.PopFloat()
	v1 := stack.PopFloat()

	if v2 == 0 {
		panic("java.lang.ArithmeticException: / by zero")
	}
	stack.PushFloat(v1 / v2)
}

type IDiv struct {
	base.NoOperandsInstruction
}

func (e *IDiv) Execute(frame *rtda.Frame) {
	stack := frame.OperandStack()
	v2 := stack.PopInt()
	v1 := stack.PopInt()

	if v2 == 0 {
		panic("java.lang.ArithmeticException: / by zero")
	}
	stack.PushInt(v1 / v2)
}

type LDiv struct {
	base.NoOperandsInstruction
}

func (e *LDiv) Execute(frame *rtda.Frame) {
	stack := frame.OperandStack()
	v2 := stack.PopLong()
	v1 := stack.PopLong()

	if v2 == 0 {
		panic("java.lang.ArithmeticException: / by zero")
	}
	stack.PushLong(v1 / v2)
}
